<?php

declare (strict_types=1);
namespace App\Model;

use Hyperf\Database\Model\Relations\HasOne;
use Hyperf\Database\Model\Relations\hasMany;

/**
 * @property int $id 
 * @property int $user_id 
 * @property int $coupon_id 
 * @property int $stock_id 
 * @property int $type 
 * @property int $expire_time 
 * @property int $use_time 
 * @property int $buy_time 
 */
class UserCoupon extends Model
{
    public $timestamps = false;

    /**
     * The table associated with the model.
     *
     * @var string
     */
    protected $table = 'user_coupon';
    /**
     * The attributes that are mass assignable.
     *
     * @var array
     */
    protected $guarded = [];
    /**
     * The attributes that should be cast to native types.
     *
     * @var array
     */
    protected $casts = ['id' => 'int', 'user_id' => 'integer', 'business_id', 'coupon_id' => 'integer', 'stock_id' => 'integer', 'type' => 'integer', 'expire_time' => 'integer', 'use_time' => 'integer', 'buy_time' => 'integer'];

    public function coupon()
    {
        return $this->belongsTo(Coupon::class, 'coupon_id');
    }

    public function stock()
    {
        return $this->hasOne(CouponStock::class, 'stock_id');
    }

    /**
     * 关联优惠券店铺
     *
     * @return hasMany
     */
    public function business() : hasMany
    {
        return $this->hasMany(Business::class, 'id','business_id');
    }

    /**
     * 封面图片获取器
     *
     * @param $value
     * @return string
     */
    public function getQrCodeAttribute($value)
    {
        return gConfig('ali_oss_domain') . $value;
    }

    /**
     * 过期时间获取器
     *
     * @param $value
     * @return false|float|int|string
     */
    public function getBuyTimeAttribute($value)
    {
        return date('Y.m.d', $value);
    }

    /**
     * 使用时间获取器
     *
     * @param $value
     * @return false|float|int|string
     */
    public function getUseTimeAttribute($value)
    {
        return date('Y-m-d H:i:s', $value);
    }

    /**
     * 核销订单关联用户模型
     *
     * @return hasOne
     */
    public function user(): HasOne
    {
        return $this->hasOne(User::class, 'id','user_id');
    }

    /**
     * 核销订单关联优惠券模型
     *
     * @return hasOne
     */
    public function couponInfo(): HasOne
    {
        return $this->hasOne(Coupon::class, 'id','coupon_id');
    }
}